package classes;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import interfaces.ICanal;
import interfaces.ICapteur;
import interfaces.IStrategy;

public class CapteurImpl extends JFrame implements ICapteur{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<ICanal> canaux;
	private JLabel label;
	private IStrategy strategie;
	private JLabel lblStrategie;
	
	public CapteurImpl(){
		lblStrategie=new JLabel();
		strategie=new StrategyEpoque(this);
		canaux=new ArrayList<ICanal>();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					buildUI();
					setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}
	
	public void setStrategie(IStrategy strategie) {
		this.strategie = strategie;
	}

	public void buildUI(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		JPanel contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCapteur = new JLabel("Capteur");
		lblCapteur.setBounds(184, 10, 65, 20);
		lblCapteur.setFont(new Font("Tahoma", Font.BOLD, 16));
		contentPane.add(lblCapteur);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.GREEN);
		panel.setBounds(184, 89, 85, 51);
		contentPane.add(panel);
		
		label = new JLabel("0");
		panel.add(label);
		label.setBackground(Color.GREEN);
		label.setFont(new Font("Tahoma", Font.BOLD, 37));
		
		lblStrategie.setBounds(10, 237, 141, 14);
		contentPane.add(lblStrategie);
	}
	
	@Override
	public void tick() {
		strategie.traiterTick();
	}

	public Point getValue(){
		return strategie.getValue();
	}

	@Override
	public void add(ICanal canal) {
		canaux.add(canal);
		
	}

	@Override
	public void remove(ICanal canal) {
		canaux.remove(canal);
		
	}

	@Override
	public int getNbCanaux() {
		return canaux.size();
	}

	@Override
	public void updateCanaux() {
		Iterator<ICanal> it=canaux.iterator();
		while(it.hasNext()){
			ICanal canal=it.next();
			canal.update(this);
		}
		
	}

	@Override
	public void setLabelText(String string) {
		label.setText(string);
		
	}

	@Override
	public void setStrategyText(String strategy) {
		lblStrategie.setText(strategy);
		
	}
	
}
